package com.kxn.multi.module.api.component;


import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextClosedEvent;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;

/**
 * 关闭进程执行相关清理动作
 *
 */
@Component
@Slf4j
public class ShutdownHookComponent implements DisposableBean, ApplicationListener<ContextClosedEvent> {

    @Override
    public void destroy() throws Exception {
        log.info("shutdown hook, disposable bean");
    }

    @Override
    public void onApplicationEvent(ContextClosedEvent event) {
        log.info("shutdown hook, ContextClosedEvent");
    }

    /**
     * 关闭jvm执行清理工作
     */
    @PostConstruct
    public void init() {
        Runtime.getRuntime().addShutdownHook(new Thread(() -> log.info("shutdown hook, jvm runtime hook")));
    }

    @PreDestroy
    public void preDestroy() {
        log.info("shutdown hook, pre destroy");
    }
}
